<template>
  <div>
    <el-tree :props="props" :load="loadNode" lazy :accordion="true" @node-click="nodeClick"></el-tree>
  </div>
</template>

<script>
import { getCategory } from "@/api";
export default {
  data() {
    return {
      props: {
        label: "name",
        children: "zones",
        isLeaf: "leaf",
      },
    };
  },
  methods: {
    async loadNode(node, resolve) {
      // node.level为0，是一级类目
      if (node.level === 0) {
        let res = await getCategory();
        return resolve(res.result);
      }
      // node.level >= 1，是子类目
      if (node.level >= 1) {
        let res = await getCategory({ id: node.data.cid });
        // 有下面的子项
        if (res.status === 200) {
          return resolve(res.result);
        } else {
          // 无子项返回空
          return resolve([]);
        }
      }
    },
    nodeClick(data, node, el) {
      this.$emit('sendTreeData', data);
    }
  },
};
</script>

<style lang="less" scoped></style>
